﻿/* ######################################## *\
 * ### Copyright (C) 2009 AJ Ravindiran ### *
 * ### HTTP://THEAJ.NET/ <AJ@THEAJ.NET> ### *
\* ######################################## */
using System;
using System.Text;
using System.Net.Sockets;
using System.Collections.Generic;

using AJRavindiran.Jolt.Utilities;
using AJRavindiran.Jolt.RuneScape.Model.Characters;
using AJRavindiran.Jolt.RuneScape.Network.Protocols;

namespace AJRavindiran.Jolt.RuneScape.Model
{
    public class CharacterManager
    {
        #region Fields
        /// <summary>
        /// When a player logs in, the initial character will be stored in this array list.
        /// <para>The max ammount of connections is capped at the max ammount of TCP connections.</para>
        /// </summary>
        private List<Character> mCharacters = new List<Character>(JoltEnvironment.GetTCPConnection().MaxConncetions);
        #endregion

        #region Properties
        #endregion

        #region Methods
        /// <summary>
        /// Assign the player's character to the connection.
        /// </summary>
        public void AddCharacter(Socket socket, int connectionID)
        {
            if (connectionID == 0)
                JoltEnvironment.GetTCPConnection().FreeConnection(connectionID);

            mCharacters.Add(new Character(socket, connectionID));
        }

        /// <summary>
        /// Removes the character from the game, and all the traces.
        /// </summary>
        public void RemoveCharacter(Character character)
        {
            character.DestroyCharacter();
            mCharacters.Remove(character);
            JoltEnvironment.GetTCPConnection().FreeConnection(character.GetDetails().ConnectionID);
        }

        /// <summary>
        /// Now that a new socket and character has been initialized, 
        /// we can now attempt to create a login protocol for the user, 
        /// so that he/she can reiceve the update keys and enter account
        /// details to verify user.
        /// </summary>
        public void ConnectPlayers()
        {
            RS2LoginProtocol loginProtocol = null;
            foreach (Character character in mCharacters)
            {
                if (loginProtocol == null)
                    loginProtocol = new RS2LoginProtocol(character);
                if (character.LoginStage == -1)
                    RemoveCharacter(character);
            }
        }

        /// <summary>
        /// A boolean which tells if the player is online or not.
        /// </summary>
        public bool PlayerOnline(string name, Character character)
        {
            foreach (Character c in mCharacters)
            {
                if (character.GetDetails().ConnectionID != c.GetDetails().ConnectionID)
                {
                    if (c.GetDetails().Username.EqualsIgnoreCase(name))
                    {
                        return true;
                    }
                }
            }
            return false;
        }
        #endregion
    }
}
